#include <iostream>

#include "bang_robot_service.h"

#include "base/logging.h"
#include "base/get_local_ip.h"
#include "base/config_reader.h"

#include "shop_info_cache.h"
#include "area_index.h"
#include "dispatch_message_task.h"

int main(int argc, char **argv) {
  char * conf_file = "../conf/bang_robot_service.conf";
  char opt;
  while ((opt = getopt(argc, argv, "c:")) != -1) {
    switch (opt) {
    case 'c':
      conf_file = optarg;
      break;
    default:
      std::cerr << "Unknown option " << optopt << std::endl;
      return 1;
    }
  }

  ConfigReader cfg(conf_file);
  if (!cfg) {
    std::cerr << "Config file " << conf_file << " read error!" << std::endl;
    return 1;
  }

  LOG_INIT(cfg.Get("log4cplus", "file"), cfg.Get("log4cplus", "level"));

  int port = cfg.GetWithType<int>("bang_robot_service", "port", 9092);
  LOG_INFO("listens on port " << port);

  using namespace imserver;

  ShopInfoCache::Instance().Reload();
  AreaIndex::Instance().Reload();
  DispatchMessageTask::StartIoService();

  melon::service::ThriftService<BangRobotServiceHandler, BangRobotServiceProcessor> service;
  service.StartRegister("/im/bang_robot_service", "1.0", 0, base::GetLocalIp(), port, 1);
  service.Start(port);
  return 0;
}

